<?php

class RequestDB{
	
	protected $pdo;
	
	public function __construct($pdo){
		
		$this->pdo = $pdo;
		
	}
	
	public function selectAll($where){
		
		$statement = $this -> pdo->prepare("select * from {$where}");
		$statement -> execute();
		return $statement -> fetchAll();
		
	}
	
	public function isRegister (){
		
		$sql = $this->pdo->prepare("select isregister from panel where id=1");
		$sql->execute();
		return $sql->fetchAll(PDO::FETCH_COLUMN, 0);
		
	}
	
	public function isAdmins ($user){
		
		$sql = $this->pdo->prepare("select isadmin from video where userName='$user'");
		$sql->execute();
		return $sql->fetchAll(PDO::FETCH_COLUMN, 0);
		
	}
	
	public function ToLogin ($account,$password){
		
		$sql = $this->pdo->prepare("select * from video where userName='$account' and pwd='$password'");
		$sql->execute();
		return $sql->fetchAll();
		
	}
	
	public function ToRegister ($name,$acc,$pwd){
		
		$sql = $this->pdo->prepare("select * from video where nickName='$name'");
		$sql-> execute();
		if($sql -> rowCount() > 0){
			return "1";
			exit();
		}
		
		$sql = $this->pdo->prepare("select * from video where userName='$acc'");
		$sql-> execute();
		if($sql -> rowCount() > 0){
			return "2";
			exit();
		}
		
		$sql = $this->pdo->prepare("select * from video where isdel=1");
		$sql-> execute();
		$isdeln =  $sql -> fetchAll();
		
		//return $isdeln[0][0];
		
		if($isdeln != null){
			$isdelid = $isdeln[0][0];
			$sql = $this->pdo->prepare("UPDATE `video` SET `userName`='$acc', `nickName`='$name', `pwd`='$pwd', `isadmin`='0', `isupload`='0', `isdel`='0' WHERE  `id`=$isdelid");
			$sql-> execute();
			if($sql -> rowCount() > 0){
				return "3";
				exit();
			}
		}else{
			$sql = $this->pdo->prepare("insert video(userName,pwd,nickName) values('$acc','$pwd','$name')");
			$sql-> execute();
			$sql = $this->pdo->prepare("insert loves(loves) values('')");
			$sql-> execute();
			if($sql -> rowCount() > 0){
				return "3";
				exit();
			}
		}
		
		/* return $sql-> fetchAll();
		 if($sql -> fetchColumn() > 0){
			return "2";
		} */
	}
	
}

?>